<div class="form-field">
<#if form.mode == "view">
    <div class="viewmode-field">
        <#if field.mandatory && !(field.value?is_number) && field.value == "">
        <span class="incomplete-warning"><img src="${url.context}/res/components/form/images/warning-16.png" title="${msg("form.field.incomplete")}" /><span>
        </#if>
        <span class="viewmode-label">${field.label?html}:</span>
        <#if field.control.params.activateLinks?? && field.control.params.activateLinks == "true">
            <#assign fieldValue=field.value?html?replace("((http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?\\^=%&:\\/~\\+#]*[\\w\\-\\@?\\^=%&\\/~\\+#])?)", "<a href=\"$1\" target=\"_blank\">$1</a>", "r")>
        <#else>
            <#if field.value?is_number>
                <#assign fieldValue=field.value?c>
            <#else>
                <#assign fieldValue=field.value?html>
            </#if>
        </#if>
        <span class="viewmode-value"><#if fieldValue == "">${msg("form.control.novalue")}<#else>${fieldValue}</#if></span>
    </div>
<#else>
    <label for="${fieldHtmlId}">${field.label?html}:<#if field.mandatory><span class="mandatory-indicator">${msg("form.required.fields.marker")}</span></#if></label>
    <input id="${fieldHtmlId}" name="${field.name}" tabindex="0" onchange="getGavDokPav(this.value)"
           <#if field.control.params.password??>type="password"<#else>type="text"</#if>
           <#if field.control.params.styleClass??>class="${field.control.params.styleClass}"</#if>
           <#if field.control.params.style??>style="${field.control.params.style}"</#if>
           <#if field.value?is_number>value="${field.value?c}"<#else>value="${field.value?html}"</#if>
           <#if field.description??>title="${field.description}"</#if>
           <#if field.control.params.maxLength??>maxlength="${field.control.params.maxLength}"</#if>
           <#if field.control.params.size??>size="${field.control.params.size}"</#if>
           <#if field.disabled && !(field.control.params.forceEditable?? && field.control.params.forceEditable == "true")>disabled="true"</#if> />
    <@formLib.renderFieldHelp field=field />
<script type="text/javascript">//<![CDATA[
var fieldValueGavKod = "${field.value}";
var idGavKod = "${fieldHtmlId}";
var fieldsGavKod = idGavKod.split(/_rcc_/);
var propertyIdGavKod = fieldsGavKod[0]+"_rcc_";
var f1GavKod = "${field.control.params.fieldGavId1}";
var f2GavKod = "${field.control.params.fieldGavId2}";
var f3GavKod = "${field.control.params.fieldGavId3}";
var fieldId1GavKod =  propertyIdGavKod+f1GavKod;
var fieldId2GavKod = propertyIdGavKod+f2GavKod;
var fieldId3GavKod = propertyIdGavKod+f3GavKod;

function changeNumber(num) {
    return num.replace(/,/g, '');
}

function trigerKeyEvent(elemId){
	if (document.createEvent) {
        var myEv = document.createEvent('KeyboardEvent');
        myEv.initKeyboardEvent('keyup', true, true, null, false, false, false, false, 76, 0);
        $('#'+elemId).each(function() {this.dispatchEvent(myEv);});
    } else if (document.createEventObject) {
		var myEv = document.createEventObject();
		$('#'+elemId).each(function() {this.fireEvent("onkeyup", myEv);});
    }
}
/*function trigerKeyEvent(elemId) {
    var myEvent = document.createEvent('KeyboardEvent');
    myEvent.initKeyboardEvent('keyup', true, true, null, false, false, false, false, 76, 0);
    $('#'+elemId).each(function() {
        this.dispatchEvent(myEvent);
    });
}*/

if (fieldValueGavKod.length > 0 && fieldValueGavKod != "" && fieldValueGavKod != null) {
    var updateOnLoadGavDokPav = function(res) {
        var result = eval('(' + res.serverResponse.responseText + ')');
        if (result) {
            if (result.Kodas.length > 0 && result.Kodas[0] != 'None' ) {
                if (result.FilialoId.length > 0 && result.FilialoId[0] != 'None') {
                    var selVal = document.getElementById(fieldId2GavKod).value;
                    $('#'+fieldId2GavKod).empty();var option = '<option value=""></option>';$('#'+fieldId2GavKod).append(option);
                    for (var j = 0; j < result.FilialoId.length; j++) {
                        var option = "<option value=" + "'" + result.FilialoId[j] + "'" + ">"+ result.FilialoPav[j] + "</option>";
                        $('#'+fieldId2GavKod).append(option);
                        if(result.FilialoId[j] === selVal) {
                            $("select#" + fieldId2GavKod + " option:eq("+(j+1)+")").attr('selected', true);
                        }
                    }
                } else {}
            } else {}
        } else {}
    };

    Alfresco.util.Ajax.jsonGet({
        url : Alfresco.constants.PROXY_URI + "doc/metadata/jarDuomenys?code="+ changeNumber(fieldValueGavKod)+"&type=jar",
        successCallback : {
            fn : updateOnLoadGavDokPav,
            scope : this
        },
        failureCallback : {
            fn : function() {},
            scope : this
        }
    });
}

function getGavDokPav(code){
    if(code != null && code != "" && code != undefined) {
        var updateGavDokPav = function(res) {
            var result = eval('(' + res.serverResponse.responseText + ')');
            if (result) {
                if (result.Kodas.length > 0 && result.Kodas[0] != 'None' ) {
                    document.getElementById(fieldId1GavKod).value = result.Pavadinimas[0];
                    var option = '<option value=""></option>';$('#'+fieldId2GavKod).empty();$('#'+fieldId3GavKod).empty();$('#'+fieldId2GavKod).append(option);$('#'+fieldId3GavKod).append(option);
                    if (result.FilialoId.length > 0 && result.FilialoId[0] != 'None') {
                        for (var j = 0; j < result.FilialoId.length; j++) {
                            var option = "<option value=" + "'" + result.FilialoId[j] + "'" + ">"+ result.FilialoPav[j] + "</option>";
                            $('#'+fieldId2GavKod).append(option);
                        }
                    } else{
                        var option = '<option value=""></option>';$('#'+fieldId2GavKod).empty();$('#'+fieldId3GavKod).empty();$('#'+fieldId2GavKod).append(option);$('#'+fieldId3GavKod).append(option);
                    }
                } else {
                    document.getElementById(fieldId1GavKod).value = "";
                    var option = '<option value=""></option>';$('#'+fieldId2GavKod).empty();$('#'+fieldId3GavKod).empty();$('#'+fieldId2GavKod).append(option);$('#'+fieldId3GavKod).append(option);
                }
            } else {
                document.getElementById(fieldId1GavKod).value = "";
                var option = '<option value=""></option>';$('#'+fieldId2GavKod).empty();$('#'+fieldId3GavKod).empty();$('#'+fieldId2GavKod).append(option);$('#'+fieldId3GavKod).append(option);
            }
            trigerKeyEvent(idGavKod);
        };

        Alfresco.util.Ajax.jsonGet({
            url : Alfresco.constants.PROXY_URI + "doc/metadata/jarDuomenys?code="+ changeNumber(code)+"&type=jar",
            successCallback : {
                fn : updateGavDokPav,
                scope : this
            },
            failureCallback : {
                fn : function() {},
                scope : this
            }
        });
    } else{}
}//]]>

/*    function onClick_dokSudKodPav(valu) {
        var propertyId = "template_x002e_edit-metadata_x002e_edit-metadata_x0023_default_prop_";
        if (valu != '' ) {
            var fieldId2 = propertyId+"rcc_dokSudRcPav";
            $('#' + fieldId2).val(valu);
        }
    }
*/
    </script>
</#if>
</div>